package com.baseweb.webadmin.service.impl;

import com.baseweb.webadmin.core.model.dao.RoleDao;
import com.baseweb.webadmin.core.model.mapper.RoleMapper;
import com.baseweb.webadmin.core.model.mapper.UserRoleMapper;
import com.baseweb.webadmin.core.model.pojo.Role;
import com.baseweb.webadmin.core.model.pojo.RoleExample;
import com.baseweb.webadmin.core.model.pojo.UserRoleKey;
import com.baseweb.webadmin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * @param userId
     * @return 用户具有的role集合
     * @author;wxy -2013-05-16
     */
    @Override
    public List<Role> selectRoleListByUser(String userId) {
        return roleDao.selectRoleListByUser(userId);
    }

    @Override
    public List<Role> selectRoles() {
        return roleDao.selectRoles();
    }

    @Override
    public Role selectRoleById(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public int insertUserRoleKey(UserRoleKey userRoleKey) {
        return userRoleMapper.insert(userRoleKey);
    }

    @Override
    public int deleteByPrimaryKey(UserRoleKey userRoleKey) {
        return userRoleMapper.deleteByPrimaryKey(userRoleKey);
    }

    @Override
    public int insertRole(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public Role selectRoleByNameAndDescn(String roleName) {
        Role role = new Role();
        role.setRoleName(roleName);
        return roleDao.selectRoleByName(role);
    }

    @Override
    public Boolean isRole(String roleName) {
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andRoleNameEqualTo(roleName);
        List<Role> roleList = roleMapper.selectByExample(roleExample);
        if (roleList.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * update wxy
     * 增加排除角色: 广告主-无线直购
     * 类型的
     *
     * @return
     */

    @Override
    public List<Role> selectLimitRoles() {
        List<Integer> roleIds = new ArrayList<Integer>();
        roleIds.add(7);
        roleIds.add(8);
        roleIds.add(9);
        roleIds.add(27);
        RoleExample roleExample = new RoleExample();
        // roleExample.createCriteria().andIdNotBetween(7l, 9l);
        roleExample.createCriteria().andIdNotIn(roleIds);
        return roleMapper.selectByExample(roleExample);
    }

    @Override
    public int deleteRoleById(Integer roleId) {
        return roleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public int updateRole(Role role) {
        return roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public List<Role> selectRolesByUser(Integer id) {
        List<Role> roles = roleDao.selectRolesByUser(id);

        List<Role> children = new ArrayList<Role>();
        for (Role role : roles) {
            children.addAll(getChildRoles(role.getId()));
        }
        roles.addAll(children);
        return roles;
    }

    @Override
    public List<Role> selectByExample(RoleExample example) {
        return roleMapper.selectByExample(example);
    }

    /**
     * 获取祖先节点
     *
     * @param id
     * @return
     */
    private List<Role> getParentRoles(Integer id) {
        if (id == 0) {
            return new ArrayList<Role>();
        } else {
            List<Role> roles = new ArrayList<Role>();
            while (true) {
                Role role = roleMapper.selectByPrimaryKey(id);
                if (role.getParentId() == 0) {
                    roles.add(role);
                    return roles;
                } else {
                    if (roles.contains(role)) {//数据有误  存在死循环
                        return roles;
                    } else {
                        roles.add(role);
                        id = role.getParentId();
                    }
                }
            }
        }
    }

    //获取指定的所有子孙节点
    public List<Role> getChildRoles(Integer id) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);

        List<Role> roles = roleMapper.selectByExample(example);

        List<Role> list = new ArrayList<Role>();
        if (roles != null && roles.size() > 0) {
            list.addAll(roles);
            for (Role role : roles) {
                list.addAll(getChildRoles(role.getId()));
            }
        }

        return list;
    }

    @Override
    public List<Role> findAllRoleByTypePage(String type,int page,int pageNum){
        return roleMapper.findAllRoleByTypePage(type,page,pageNum);
    }

    @Override
    public int countRoleByType(String type){
        return roleMapper.countRoleByType(type);
    }

    @Override
    public Role findRoleByCode(String code){
        return roleMapper.findRoleByCode(code);
    }

    @Override
    public int insertIntoRoleUser(int userId,int roleId){
        return roleMapper.insertIntoRoleUser(userId,roleId);
    }

    @Override
    public int updateIntoRoleUser(int roleId,int userId,String roleType){
        return roleMapper.updateIntoRoleUser(roleId,userId,roleType);
    }

    @Override
    public Role findRoleByUserIdType(int userId,String type){
        return roleMapper.findRoleByUserIdType(userId,type);
    }
}
